<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
// 
// 
/**
 * 获取指定月份的第一天开始和最后一天结束的时间戳
 *
 * @param int $y 年份 $m 月份
 * @return array(本月开始时间，本月结束时间)
 */
 
 function mFristAndLast($y = "", $m = ""){
    if ($y == "") $y = date("Y");
    if ($m == "") $m = date("m");
    $m = sprintf("%02d", intval($m)); 
    $y = str_pad(intval($y), 4, "0", STR_PAD_RIGHT); 
    $m>12 || $m<1 ? $m=1 : $m=$m;
    $firstday = strtotime($y . $m . "01000000");
    $firstdaystr = date("Y-m-01", $firstday);
    $lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
 
    return array(
        "firstday" => $firstday,
        "lastday" => $lastday
    );
}

 //https请求（支持GET和POST）
    function https_request($url, $data = null) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        $output_json=json_decode($output,true);
        return $output_json;
    }

    /*
    * api友好输出
    */
    function api_return($code,$data=[],$msg='暂无'){
        $code_config = config('base.code');
        $return_data['code'] = $code??-2017;
        $return_data['msg'] =  $code_config[$code]??$msg;
        $return_data['data'] = $data;
        return $return_data;
    }



     /**
    * 根据地址获取经纬度
    *
    */
     function getLatng($prepAddr){
        $key = config("baiduKey");
        $geocode=file_get_contents("http://api.map.baidu.com/geocoder/v2/?address=".$prepAddr."&output=json&ak=".$key);
        $output= json_decode($geocode,true);  
        if($output['status'] == 0){
          $latng = $output['result']['location'];
          return $latng;
        }else{
            return false;
        }
    }

   /**
    * 设置缓存
    * @param $key 键值
    * @param $value 缓存的值
    */
    function set_cache($key,$values){
       cache($key,$values);
    }

   /**
    * 获取缓存
    * @param $key 键值
    */
    function get_cache($key){
       return cache($key);
    }
